<?php

    /**
     * 昵称生成规则
     *
     * @author : chenhan
     * @access public|private|protected
     *
     * @param  mixed $phone 手机号
     *
     * @return string
     */
    function nickname($phone)
    {
        if (empty($phone)) {
            $phone = rand(1111, 9999);
        }
        $str = '';
    //    $str = 'TLW'.substr($phone,-3,3).implode(array_reverse(str_split(substr($phone,3,3))));
        //$str = 'TLW'.rand(11,99).substr($phone,-4,4);
        $str = 'TLW_' . date('Ymd', time()) . rand(1111, 9999);

        return $str;
    }

    /**
     * 获取客户端IP地址
     *
     * @param integer $type 返回类型 0 返回IP地址 1 返回IPV4地址数字
     * @param boolean $adv  是否进行高级模式获取（有可能被伪装）
     *
     * @return mixed
     */
    function get_client_ip($type = 0, $adv = false)
    {
        $type = $type ? 1 : 0;
        static $ip = NULL;
        if ($ip !== NULL) return $ip[ $type ];
        if ($adv) {
            if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
                $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
                $pos = array_search('unknown', $arr);
                if (false !== $pos) unset($arr[ $pos ]);
                $ip = trim($arr[0]);
            } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
                $ip = $_SERVER['HTTP_CLIENT_IP'];
            } elseif (isset($_SERVER['REMOTE_ADDR'])) {
                $ip = $_SERVER['REMOTE_ADDR'];
            }
        } elseif (isset($_SERVER['REMOTE_ADDR'])) {
            $ip = $_SERVER['REMOTE_ADDR'];
        }
        // IP地址合法验证
        $long = sprintf("%u", ip2long($ip));
        $ip = $long ? [$ip, $long] : ['0.0.0.0', 0];
        return $ip[ $type ];
    }
    /*oppo回调处理*/
    function get_param($param_name)   //兼容post/get
    {
        $param_value = "";
        if(isset($_POST[$param_name])){
            $param_value = trim($_POST[$param_name]);
        }else if(isset($_GET[$param_name])){
            $param_value = trim($_GET[$param_name]);
        }
        return $param_value;
    }
    function rsa_verify($contents,$publickey) {
        $str_contents = "notifyId={$contents['notifyId']}&partnerOrder={$contents['partnerOrder']}&productName={$contents['productName']}&productDesc={$contents['productDesc']}&price={$contents['price']}&count={$contents['count']}&attach={$contents['attach']}";
        //$publickey= $this->config['OPPO']['PUBLIC_KEY'];
        $pem = chunk_split($publickey,64,"\n");
        $pem = "-----BEGIN PUBLIC KEY-----\n".$pem."-----END PUBLIC KEY-----\n";
        $public_key_id = openssl_pkey_get_public($pem);
        $signature =base64_decode($contents['sign']);
        return openssl_verify($str_contents, $signature, $public_key_id );   //成功返回1,0失败，-1错误,其他看手册
    }
    /*华为生成签名*/
    function rsa_sign($data,$priKey) {
        $res = "-----BEGIN RSA PRIVATE KEY-----\n" .
            wordwrap($priKey, 64, "\n", true) .
            "\n-----END RSA PRIVATE KEY-----";
        openssl_sign ( $data, $sign, $res , OPENSSL_ALGO_SHA256);
        $sign = base64_encode ( $sign );
        return $sign;
    }
    /*华为验签*/
    function hw_verify($data, $sign,$pubKey){
        /*测试*/
        //$pubKey= "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAIW1g+KAqqOeC1ypte8L3qTDk2nz6jUbM6o6Jg9obvivPnCAm/wZvV3jWbYWfOuO/wrFJygn/jZqf8cR1T1CQa8CAwEAAQ==";
        $res = "-----BEGIN PUBLIC KEY-----\n" .
            wordwrap($pubKey, 64, "\n", true) .
            "\n-----END PUBLIC KEY-----";
        $result = (openssl_verify($data, base64_decode($sign), $res, OPENSSL_ALGO_SHA256)===1);
        return $result;
    }
    
    /**
     * redis连接
     * @ author: zhangzhiyuan
     * @ email: fh515934402@163.common
     * @ date: 2017/8/17 19:25
     */
    function connect_redis(){
         $redis = new Redis();
         $redis->connect(config('REDIS_FORMAL.host'), config('REDIS_FORMAL.port'));
         $redis->auth(config('REDIS_FORMAL.auth'));
         $redis->select(2);
         return $redis;
    }
    /**
     * redis连接
     * @ author: zhangzhiyuan
     * @ email: fh515934402@163.common
     * @ date: 2017/8/17 19:25
     */
    function connect_redis_db_0(){
        $redis = new Redis();
        $redis->connect(config('REDIS_FORMAL.host'), config('REDIS_FORMAL.port'));
        $redis->auth(config('REDIS_FORMAL.auth'));
        $redis->select('0');
        return $redis;
    }
    /**
     * 获取充值额外参数
     * @ author: zhangzhiyuan
     * @ email: fh515934402@163.common
     * @ date: 2017/8/17 19:25
     */
    function get_redis_value($UserID){
        $redis = connect_redis();
        $value =$redis->hGet('recharge_user',$UserID);
        return $value;
    }
    /**
     * 获取充值额外参数
     * @ author: zhangzhiyuan
     * @ email: fh515934402@163.common
     * @ date: 2017/8/17 19:25
     */
    function set_redis_value($UserID,$extra){
        $redis = connect_redis();
        $redis->hset('recharge_user',$UserID, $extra);
        return true;
    }
    /**
     * 获取充值额外参数
     * @ author: zhangzhiyuan
     * @ email: fh515934402@163.common
     * @ date: 2017/8/17 19:25
     */
    function del_redis_value($UserID){
        $redis = connect_redis();
        $redis->hDel('recharge_user',$UserID);
        return true;
    }
    /**
     * 龙王币（[10186]）
     * @author zhangzhiyuan
     * @return string json
     */
    function Daily_limited_time_hyperion_prop($UserID,$Time_prop_arr){
        $redis =connect_redis_db_0();
        $res =$redis->zScore(env('TIME_PROPS.hyperion').'2020'.date('W'),$UserID);
        /*道具存储*/
        if($res){
            $score=$Time_prop_arr[0][1]+$res;
            $redis->zAdd(env('TIME_PROPS.hyperion').'2020'.date('W'),$score,$UserID);
        }else{
            $redis->zAdd(env('TIME_PROPS.hyperion').'2020'.date('W'),$Time_prop_arr[0][1],$UserID);
        }
        return true;
    }


    // 获取某个时间所在的月份 默认获取当前天
    function getCurrentMonth($day = null){
        if(is_null($day)){
            return date('Ym');
        }

        return date('Ym',strtotime($day));
        
    }

    // 根据月份查询不同的分表
    function getPropLogTable($day = null){
        return 'user_prop_log_'.getCurrentMonth($day);
    }

    function getScoreLogTable($day = null){
        return 'user_score_log_'.getCurrentMonth($day);
    }



